人總是會有犯錯的一天,
程式想必也是如此,
但犯錯之後該怎麼辦呢?
我們今天來探討C#的錯誤處理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告分子
            int x = 100;
            //宣告分母
            int y = 0;
            int z = x / y;
            Console.WriteLine("結果 : " + z);
            Console.ReadKey();
        }
    }
  }
結果:
未處理的例外狀況: System.DivideByZeroException: 嘗試以零除。
於 CsharpDemo.Program.Main(String[] args) 於 C:\CsharpDemo\Program.cs: 行 21
我們會發現程式拋出了一個DivideByZeroException類別的錯誤,來提醒開發者發生什麼事情了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("請輸入分母");
                //宣告分子
                int x = 100;
                //宣告分母
                int y = Convert.ToInt32(Console.ReadLine());
                int z = x / y;
                Console.WriteLine("結果 : " + z);
            }
            catch (DivideByZeroException)//捕捉分母為0的錯誤
            {
                Console.WriteLine("分母不可以為0");
            }
            catch (FormatException)//捕捉格式錯誤
            {
                Console.WriteLine("格式不正確");
            }
            Console.ReadKey();
        }
    }
  }
輸入:
0
結果:
請輸入分母
0
分母不可以為0
輸入:
孤獨一隻雞
結果:
請輸入分母
孤獨一隻雞
格式不正確
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("請輸入分母");
                //宣告分子
                int x = 100;
                //宣告分母
                int y = Convert.ToInt32(Console.ReadLine());
                int z = x / y;
                Console.WriteLine("結果 : " + z);
            }
            catch (Exception ex)//捕捉所有發生的錯誤,如果不知道可能會發生的錯誤可以使用此類別
            {
                //ex.Message可得到簡易的錯誤描述,如果需要詳情,建議使用 ex.ToString()
                Console.WriteLine(ex.Message);
            }
            finally//最終要處理的事情
            {
                Console.WriteLine("既然都犯錯了,那就收工吃飯吧!");
            }
            Console.ReadKey();
        }
    }
  }
輸入:
0
結果:
請輸入分母
0
嘗試以零除。
既然都犯錯了,那就收工吃飯吧!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("請輸入1~10任一數");
                int number = Convert.ToInt32(Console.ReadLine());
                //判斷是否有超過範圍
                if (number < 1 || number > 10)
                {
                    //拋出超出範圍的異常
                    throw new IndexOutOfRangeException();
                }
                else
                {
                    Console.WriteLine("你輸入了 : " + number);
                }
            }
            catch (Exception ex)//捕捉所有發生的錯誤,如果不知道可能會發生的錯誤可以使用此類別
            {
                //ex.Message可得到簡易的錯誤描述,如果需要詳情,建議使用 ex.ToString()
                Console.WriteLine(ex.Message);
            }
            finally//最終要處理的事情
            {
                Console.WriteLine("既然都犯錯了,那就收工吃飯吧!");
            }
            Console.ReadKey();
        }
    }
  }
輸入:
11
結果:
請輸入1~10任一數
11
索引在陣列的界限之外。
既然都犯錯了,那就收工吃飯吧!